/*************************************************************************
	> File Name: transport.cpp
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年04月12日 星期四 15时07分40秒
 ************************************************************************/

#include "transports.h"
#include <stdlib.h>
#include <utils/custom.h>

transports::transports(int id):
    dbms("das.db","transport",id) {

}

BOOL transports::inorup(S32 sample, U64 regtime, S32 timeout, S32 mode, const char *addr)
{
    if (!exist()) {
        return insert(sample,regtime,timeout,mode,addr);
    }
    else
        return update(sample,regtime,timeout,mode,addr);
}

BOOL transports::insert(S32 sample, U64 regtime, S32 timeout, S32 mode, const char *addr)
{
    char cond[MAX_COND_LEN] = {0};

    sprintf(cond,"%d,%d,%lld,%d,%d,'%s'",m_iId,sample,regtime,timeout,mode,addr);
    return m_sqlite.Insert("id,sample,regtime,timeout,mode,addr",cond);
}

BOOL transports::update(S32 sample, U64 regtime, S32 timeout, S32 mode, const char *addr)
{
    char cond[MAX_COND_LEN] = {0};

    sprintf(cond,"sample=%d,regtime=%lld,timeout=%d,mode=%d,addr='%s'",sample,regtime,timeout,mode,addr);
    return m_sqlite.Update(cond,getcondition().c_str());
}

void transports::assignvalue(int argc, char **argv, char **azColName)
{
#define STR_EQUAL(s1,s2) !strcmp(s1,s2)
    transmgr_t dev;
    for (int i = 0;i < argc;i++) {
        if (argv[i] == NULL)
            continue;
        if (STR_EQUAL(azColName[i],"id")) {
            dev.id = atoi(argv[i]);
        }
        else if (STR_EQUAL(azColName[i],"sample")) {
            dev.sample = atof(argv[i]);
        }
        else if (STR_EQUAL(azColName[i],"timeout")) {
            dev.timeout = atoi(argv[i]);
        }
        else if (STR_EQUAL(azColName[i],"mode")) {
            dev.trans_mode = (TM_MODE)atoi(argv[i]);
        }
        else if (STR_EQUAL(azColName[i],"addr")) {
            std::vector<std::string> list = utils::Split(argv[i],":");

            if (dev.trans_mode == TM_TCP ) {
                if (list.size() != 2) {
                    return;
                }
                strcpy(dev.trans_addr.socketaddr.ip_addr,
                       list.at(0).c_str());
                dev.trans_addr.socketaddr.port =
                        atoi(((const string&)list.at(1)).c_str());
            }
            else if (dev.trans_mode == TM_SEARIAL){
                if (list.size() != 5)
                    return;
                strcpy(dev.trans_addr.portaddr.com,
                       list.at(0).c_str());
                dev.trans_addr.portaddr.baudrate = atoi(list.at(1).c_str());
                dev.trans_addr.portaddr.databit = atoi(list.at(2).c_str());
                dev.trans_addr.portaddr.parity = atoi(list.at(3).c_str());
                dev.trans_addr.portaddr.stopbit = atoi(list.at(4).c_str());
            }
        }
    }
    m_itrans_infos.push_back(dev);
}

void transports::clearall()
{
    m_itrans_infos.clear();
}

S32 transports::fsample()
{
    string strsample = operator []("sample");
    if (!strsample.empty())
        return atoi(strsample.c_str());
    return 0;
}

int transports::itimeout()
{
    string strsample = operator []("timeout");
    if (!strsample.empty())
        return atoi(strsample.c_str());
    return 0;
}

int transports::imode()
{
    string strsample = operator []("mode");
    if (!strsample.empty())
        return atoi(strsample.c_str());
    return 0;
}

transmgr_t transports::gettransport_by_chn(const string &chn)
{
    if (chn == "tcp")
        FetchItems("*","mode=0");
    else
        FetchItems("*","mode=1 and addr like '%"+chn+"%'");

    return gettransinfo();
}
